package com.report.service;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.operator.*;
import cn.zhxu.bs.util.MapBuilder;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.report.dto.request.ReportItemReq;
import com.report.dto.response.zu.ReportItemRes;
import com.report.entity.Search;
import com.zmh.framework.constants.Constants;
import lombok.AllArgsConstructor;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2024/7/25 20:55
 */
@Service
@AllArgsConstructor
public class ReportItemService {


    private final  BeanSearcher beanSearcher;



    public Page<ReportItemRes> findReportList(ReportItemReq reportReq) {
        reportReq=getSecurityValue(reportReq);
        Page<ReportItemRes> page = new Page<>(reportReq.getPageNum(), reportReq.getPageSize());
        MapBuilder op = MapUtils.builder().page(reportReq.getPageNum() - 1, reportReq.getPageSize())
                .field(ReportItemRes::getSecurity, reportReq.getSecurity()).op(Equal.class)
                .field(ReportItemRes::getIndustryId, reportReq.getIndustryIds()).op(InList.class)
                .field(ReportItemRes::getRecentId, reportReq.getReportIds()).op(InList.class)
                .field(ReportItemRes::getCreateTime, reportReq.getReportDate()).op(Between.class)
                .field(ReportItemRes::getDeep,reportReq.getDeep()).op(Equal.class)
               .field(ReportItemRes::getName,reportReq.getSearchName()).op(Contain.class)
                 .field(ReportItemRes::getTodaySell, reportReq.getTodaySell()).op(Equal.class)
                .field(ReportItemRes::getRanking, reportReq.getRanking()).op(Equal.class)
                .field(ReportItemRes::getLanguage,reportReq.getLanguage()).op(Equal.class)
                .field(ReportItemRes::getPages,reportReq.getReportPages()).op(GreaterEqual.class)
                .field(ReportItemRes::getTypeId,reportReq.getTypeId()).op(Equal.class).field(ReportItemRes::getDeleted,Constants.ZERO).op(Equal.class)
                .field(ReportItemRes::getFileName).sql("$1 IS NOT NULl")
               ;
        if(ObjectUtil.isNotEmpty(reportReq.getSearchBoolean())){
            if(reportReq.getSearchBoolean()){
                op.field(ReportItemRes::getName,reportReq.getSearchName()).op(Contain.class);
            }else{
                op.field(ReportItemRes::getName,reportReq.getSearchName()).op(Equal.class);

            }
        }
        if(reportReq.getAsc().equals(Constants.ONE)){
            op.orderBy(ReportItemRes::getCreateTime,"DESC");
        }else  if(reportReq.getAsc().equals(Constants.TWO)){
            op.orderBy(ReportItemRes::getCreateTime,"ASC");
        }else{
            op.orderBy(ReportItemRes::getCreateTime,"DESC");
        }
        SearchResult<ReportItemRes> search = beanSearcher.search(ReportItemRes.class, timeMapBuilder(op,reportReq).build());
        page.setTotal(search.getTotalCount().longValue());
        page.setRecords(search.getDataList());

        //保存搜索条件信息
        if(StrUtil.isNotEmpty(reportReq.getSearchName())){
            Search search1=new Search();
            search1.setName(reportReq.getSearchName());
            if(StpUtil.isLogin()){
                search1.setMemberId(StpUtil.getLoginIdAsInt());
            }
            searchService.saveSearch(search1);
        }


        return page;
    }


    private final ISearchService searchService;

    private MapBuilder timeMapBuilder(MapBuilder mapBuilder,ReportItemReq reportItemReq){

        //一周内 SELECT * FROM table WHERE date(时间字段) >= DATE_SUB(CURDATE(),INTERVAL 7 DAY)
        if(reportItemReq.getTimeType().equals(Constants.ONE)){
            mapBuilder.field(ReportItemRes::getCreateTime).sql("$1 >=DATE_SUB(CURDATE(),INTERVAL 7 DAY)");
        }
        //一个月内
        if(reportItemReq.getTimeType().equals(2)){
            mapBuilder.field(ReportItemRes::getCreateTime).sql("$1 >=DATE_SUB(CURDATE(),INTERVAL 1 MONTH))");

        }
        //半年内
        if(reportItemReq.getTimeType().equals(3)){
            mapBuilder.field(ReportItemRes::getCreateTime).sql("$1  > CURDATE() - INTERVAL 180 DAY");

        }
        //一年内
        if(reportItemReq.getTimeType().equals(4)){
            mapBuilder.field(ReportItemRes::getCreateTime).sql("$1 BETWEEN DATE_SUB(NOW(), INTERVAL 1 YEAR) AND NOW()");

        }



        return mapBuilder;
    }


    private ReportItemReq getSecurityValue(ReportItemReq reportItemReq) {
        List<Integer> securityIds = reportItemReq.getSecurityIds();
        if(CollUtil.isNotEmpty(securityIds)){
            boolean b = securityIds.stream().anyMatch(securityId -> securityId == 2);
            if(b){
                reportItemReq.setSecurity(2);
            }

          if(securityIds.stream().anyMatch(securityId -> securityId == 0)){
              reportItemReq.setDeep(1);
          }
            if(securityIds.stream().anyMatch(securityId -> securityId == 3)){
                reportItemReq.setLanguage(1);
            }

            if(securityIds.stream().anyMatch(securityId -> securityId == 4)){
                reportItemReq.setTodaySell(1);
            }

            if(securityIds.stream().anyMatch(securityId -> securityId == 5)){
                reportItemReq.setRanking(1);
            }

        }
        return reportItemReq;
    }

    public Page<ReportItemRes> pdfLists(ReportItemReq reportReq) {
        Page<ReportItemRes> page = new Page<>(reportReq.getPageNum(), reportReq.getPageSize());
        Map<String, Object> build = MapUtils.builder().orderBy(ReportItemRes::getCreateTime, "DESC").build();
        SearchResult<ReportItemRes> search = beanSearcher.search(ReportItemRes.class, build);
        page.setTotal(search.getTotalCount().longValue());
        page.setRecords(search.getDataList());
        return page;
    }

    public Page<ReportItemRes> reportList(ReportItemReq reportReq) {
        Page<ReportItemRes> page = new Page<>(reportReq.getPageNum(), reportReq.getPageSize());
       Map<String, Object> build = MapUtils.builder() .page(reportReq.getPageNum()-1, reportReq.getPageSize()).field(ReportItemRes::getName,reportReq.getSearchName()).op(Contain.class)
               .field(ReportItemRes::getIndustryId,reportReq.getIndustryId()).op(Equal.class).field(ReportItemRes::getDeleted,Constants.ZERO).op(Equal.class)
               .orderBy(ReportItemRes::getCreateTime, "DESC").build();
        SearchResult<ReportItemRes> search = beanSearcher.search(ReportItemRes.class, build);
        page.setTotal(search.getTotalCount().longValue());
        page.setRecords(search.getDataList());
        return page;
     }
}
